﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using ReactiveLang;

namespace TimeZoneConverter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        TimeZoneInfo localTimeZone;
        public MainWindow()
        {
            InitializeComponent();

            srcTime.Text = DateTime.Now.ToLongTimeString();

            foreach (var tz in TimeZoneInfo.GetSystemTimeZones())
            {
                targetTimeZone.Items.Add(tz);
                if (tz.DisplayName.Contains("Tashkent"))
                    targetTimeZone.SelectedItem = tz;

                srcTimeZone.Items.Add(tz);
                if (tz.BaseUtcOffset == TimeZoneInfo.Local.BaseUtcOffset)
                    srcTimeZone.SelectedItem = localTimeZone = tz;
            }
        }

        [Reactive(AutoRun = true)]
        void ReactiveMethod()
        {
            // variable for the time that will be used as a source of conversion
            // in fact it will be stored in the heap
            DateTime sourceTime;

            // remember that the code in "live method" is interpreted differently:
            // every expression becomes observable
            // every statement becomes a long-life rule that is executed until disabled or collected by the GC


            // here is "if" statement, but it can be considered as "when" rather than "if" because it is continuous
            // when "use current" is checked
            if (useCurrentTime.IsChecked == true)
            {
                // srcTime and srcTimeZones must be disabled
                srcTime.IsEnabled = false;
                srcTimeZone.IsEnabled = false;
                // note that wrote "must" emphasizing declarativeness of rules

                // source time zone must be local
                srcTimeZone.SelectedItem = localTimeZone;

                // sourceTime must be current
                sourceTime = DateTime.Now;

                // the srcTime text box must show the source time
                srcTime.Text = sourceTime.ToLongTimeString();
            }
            else if (!DateTime.TryParse(srcTime.Text, out sourceTime)) // otherwise, when srcTime.Text cannot be parsed as DateTime
            {
                // background of srcTime must be pink
                srcTime.Background = Brushes.LightPink;
                // when the user enters a valid time to the textbox, this rule will be disabled. 
                // It will undo its changes and the background will become white

                // rules below must not work
                return;
            }

            // let sourceTimeZone and destinationTimeZone be current source and destination time zones
            var sourceTimeZone = (TimeZoneInfo)this.srcTimeZone.SelectedItem;
            var destinationTimeZone = (TimeZoneInfo)this.targetTimeZone.SelectedItem;

            // when time zones are not nulls
            if (sourceTimeZone != null && destinationTimeZone != null)
            {
                sourceTime = DateTime.SpecifyKind(sourceTime, DateTimeKind.Unspecified);
                // targetTime.Text must be the source time converted from the source time zone to the target time zone
                this.targetTime.Text = TimeZoneInfo.ConvertTime(sourceTime, sourceTimeZone, destinationTimeZone).ToLongTimeString();
            }
        }
    }
}
